import { defineStore } from 'pinia'
let user = localStorage.getItem('user')
let userdata:any = null
if (user) {
  userdata = JSON.parse(user);
}
export const userStore = defineStore({
  id: 'user',
  state: () => {
    return {
       userName: userdata?.userName||'',
       num:userdata?.num||0,
       token:userdata?.token||''
    }
  },
  actions: {
    settoken(token:any) {
      this.token = token
    },
    updateUserName(userName:any) {
      this.userName = userName
    },
    usePermissionStoreHook(num:any) {
      this.num= num
    },
  },
   // 开启数据缓存
  persist: { //数据默认存在 sessionStorage 里，并且会以 store 的 id 作为 key
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: localStorage,
      }
    ]
  }
})
